<template>
  <div id="handleDialog">
    <el-dialog title="添加分类" :visible.sync="handleDialogData.isShow">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="关键字" prop="keyword">
          <el-input style="width: 310px" v-model="ruleForm.keyword"></el-input>
        </el-form-item>
        <el-form-item label="回复内容" prop="reply">
          <el-input style="width: 310px" v-model="ruleForm.reply"></el-input>
        </el-form-item>
        <el-form-item label="链接地址" prop="link">
          <el-input style="width: 310px" v-model="ruleForm.link"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="success" @click="confirmHandle('ruleForm')"
          >确 定</el-button
        >
        <el-button @click="handleDialogData.isShow = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  addLiveKeyword,
  editLiveKeyword
} from '@/api/classifyManage/liveKeywrod'
export default {
  props: {
    handleDialogData: {
      type: Object,
      default: () => {
        return {
          isShow: false,
          params: ''
        }
      }
    }
  },
  data() {
    return {
      ruleForm: {
        keyword: '',
        reply: '',
        link: ''
      },
      rules: {
        keyword: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        reply: [{ required: true, message: '请输入显示序号', trigger: 'blur' }]
      }
    }
  },
  watch: {},
  mounted() {
    if (this.handleDialogData.type === 'edit') {
      this.ruleForm = Object.assign(this.handleDialogData.params)
    }
  },
  methods: {
    // 点击确定
    confirmHandle(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.handleDialogData.type === 'add') {
            addLiveKeyword(
              this.ruleForm,
              (response) => {
                this.$emit('refreshList')
                this.handleDialogData.isShow = false
              },
              (response) => {
                this.$message({
                  message: response.message,
                  type: 'warning'
                })
              }
            )
          } else {
            editLiveKeyword(
              this.ruleForm,
              (response) => {
                this.$emit('refreshList')
                this.handleDialogData.isShow = false
              },
              (response) => {
                this.$message({
                  message: response.message,
                  type: 'warning'
                })
              }
            )
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="less" scoped></style>
